package com.binaryTree;

import java.util.ArrayList;
import java.util.List;

public class PathSumII0304_113 {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) return res;

        List<Integer> path = new ArrayList<>();
        preorderdfs(root, targetSum, res,path);
        return res;
    }

    private void preorderdfs(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
        path.add(root.val);

        if(root.left == null && root.right == null && targetSum - root.val == 0){
            res.add(new ArrayList<>(path));
        }
        if(root.left != null){
            preorderdfs(root.left,targetSum-root.val,res,path);
            path.remove(path.size() - 1);
        }
        if (root.right != null){
            preorderdfs(root.right, targetSum-root.val,res, path);
            path.remove(path.size() -1);
        }
    }
}
